package com.my.blog.myblog.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.blog.annotation.SystemLog;
import com.my.blog.myblog.entity.Links;
import com.my.blog.myblog.entity.custom.StatusCount;
import com.my.blog.myblog.service.LinksService;
import com.my.blog.utils.ConstantUtil;
import com.my.blog.utils.ResultBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/3 15:26
 * @Description： 后台友链的controller
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminLinksController {

    @Resource
    private LinksService linksService;

    //查询还未处理友链数
    @GetMapping("/selectNoApplyLinksCount")
    public ResultBean<Object> selectNoApplyLinksCount(){
        QueryWrapper<Links> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(Links::getIsApply,"-1");
        int count = linksService.count(wrapper);
        return new ResultBean<>(count);
    }

    //分页显示友链集合
    @PostMapping("/selectLikeLinksListByPage")
    public ResultBean<IPage<Links>> selectLikeLinksListByPage(Links links,
                                                              @RequestParam(value = "page")Integer page,
                                                              @RequestParam(value = "pageSize")Integer pageSize){
        QueryWrapper<Links> linksQueryWrapper=new QueryWrapper<>();
        System.out.println("进来了....");
        System.out.println(links);
        if(ObjectUtil.isNotEmpty(links.getName())){
            linksQueryWrapper.lambda().eq(Links::getName,links.getName());
        }
        if(ObjectUtil.isNotEmpty(links.getStatus())&&links.getStatus()!=0){
            linksQueryWrapper.lambda().eq(Links::getStatus,links.getStatus());
        }
        if(ObjectUtil.isNotEmpty(links.getIsApply())){
            linksQueryWrapper.lambda().eq(Links::getIsApply,links.getIsApply());
        }

        Page<Links> linksPage = linksService.page(new Page<>(page, pageSize), linksQueryWrapper);
        ResultBean<IPage<Links>> resultBean=new ResultBean<>();
        //System.out.println(linksPage.getRecords());
        if(ObjectUtil.isEmpty(linksPage.getRecords())){
            resultBean.setCode(ResultBean.FAIL);
            System.out.println("查出的值为null");
            linksPage.setTotal(0);
            resultBean.setData(linksPage);
        }else {
            resultBean.setData(linksPage);
            resultBean.setCode(ResultBean.SUCCESS);
        }
        return resultBean;
    }

    //查询两种状态下友链总数
    @PostMapping("/selectLinksListByStatus")
    public ResultBean<List<StatusCount>> selectLinksListByStatus(){
        QueryWrapper<Links> linksQueryWrapper=new QueryWrapper<>();
        List<StatusCount> scList=new ArrayList<>();

        linksQueryWrapper.lambda().eq(Links::getIsApply,1).eq(Links::getStatus,-1);
        int count0 = linksService.count(linksQueryWrapper);
        linksQueryWrapper.clear();

        linksQueryWrapper.lambda().eq(Links::getIsApply,1).eq(Links::getStatus,1);
        int count1 = linksService.count(linksQueryWrapper);

        scList.add(new StatusCount(-1,count0));
        scList.add(new StatusCount(1,count1));
        ResultBean<List<StatusCount>> resultBean=new ResultBean<>();
        resultBean.setData(scList);
        return resultBean;
    }

    @RequestMapping("/selectLinksByIdAdmin")
    public ResultBean<Links> selectLinksById(@RequestParam(value = "id")Integer id){
        Links links = linksService.getById(id);
        ResultBean<Links> resultBean=new ResultBean<>();
        if(ObjectUtil.isNotEmpty(links)){
            resultBean.setData(links);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/updateLinks")
    @SystemLog(description = ConstantUtil.LINKS_UPDATE,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updateLinks(Links links){
        boolean b = linksService.updateById(links);
        ResultBean<Object> resultBean=new ResultBean<>();
        if(b){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.NO_PERMISSION);
        }
        return resultBean;
    }

    @PostMapping("/addLinks")
    @SystemLog(description = ConstantUtil.LINKS_ADD,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> addLinks(Links links){
        ResultBean<Object> resultBean=new ResultBean<>();
        links.setAddTime(LocalDateTime.now());
        if(linksService.save(links)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/deleteLinks")
    @SystemLog(description = ConstantUtil.LINKS_DELETE,userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> delLinks(Integer id){
        ResultBean<Object> resultBean=new ResultBean<>();
        if(linksService.removeById(id)){
            resultBean.setCode(ResultBean.SUCCESS);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
